<?php

class generalFunction {

     function getCurrentScriptName() {
          $scriptArray = explode('/', $_SERVER['SCRIPT_NAME']);
          if (count($scriptArray) > 0) {
               $script_name = end($scriptArray);
               return $script_name;
          }
          return '';
     }

     function navigationArray($key = '') {
          $navigationArray = Array();
          $navigationArray['home'] = Array('dashboard.php');
          $navigationArray['content'] = Array('content.php', 'contentlisting.php');
          $navigationArray['event'] = Array('event.php', 'eventlisting.php');
          $navigationArray['hotel'] = Array('hotel.php', 'hotellisting.php');
          $navigationArray['golf'] = Array('golf.php', 'golflisting.php');
          $navigationArray['dining'] = Array('dining.php', 'dininglisting.php');
          $navigationArray['spa'] = Array('spa.php', 'spalisting.php');
          $navigationArray['preference'] = Array('preference.php','changepassword.php', 'changeemail.php');
          $navigationArray['map'] = Array('map.php');
          

          if (array_key_exists($key, $navigationArray)) {
               return $this->navigationSelector($navigationArray[$key]);
          }
          return $this->navigationSelector($navigationArray['home']);
     }

     function navigationSelector($currentScriptArray = Array()) {
          if (in_array($this->getCurrentScriptName(), $currentScriptArray)) {
               return true;
          }
          return false;
     }

     function filter($array = '', $filterArray = Array()) {
          /*
            for the filter array the value with  following key will call following functionms
            mysqlRealEscape => mysql_real_escape_string
            htmlEntityDecode => html_entity_decode
            htmlEntity => htmlentities
            trim => TrimElement
            stripslashes => StripSlashElement
           */
          foreach ($filterArray as $filterType) {
               if (is_array($array)) {
                    $array = array_map($filterType, $array);
               } else {
                    $array = $filterType($array);
               }
          }
          return $array;
     }

     function redirect($url) {
          if ($url == '') {
               echo "redirect url cannot be empty";
               exit;
          }
          header('Location: ' . $url);
          exit;
     }

     function isLoggedIn() {
          if ($_SESSION) {
               if (isset($_SESSION['rto12']['admin']['userid']) && $_SESSION['rto12']['admin']['userid'] != '') {
                    return true;
               }
          }
          return false;
     }

     function LogOut() {
          if ($_SESSION) {
               unset($_SESSION['rto12']['admin']);
          }
          $url = $BASE_HREF . "login.php";
          $this->redirect($url);
          exit;
     }

     function set_session($data) {
          $_SESSION['rto12']['admin']['userid'] = $data['userid'];
          $_SESSION['rto12']['admin']['username'] = $data['username'];
          $_SESSION['rto12']['admin']['firstname'] = $data['firstname'];
          $_SESSION['rto12']['admin']['lastname'] = $data['lastname'];
          $_SESSION['rto12']['admin']['email'] = $data['email'];
     }
	
	
	function ValidUrl($url) {
		$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
    		$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
    		$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
    		$regex .= "(\:[0-9]{2,5})?"; // Port
    		$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
    		$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
    		$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 
		$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
		if(preg_match("/^$regex$/", $url)){
               return true;
       	}
		return false;   
	}
	     
     function ValidEmailAddress($EmailAddress) {
          if (!preg_match("/^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i", $EmailAddress)) {
               return false;
          }
          return true;
     }

     function isNumeric($n){
          return ( $n == strval(intval($n)) )? true : false;
     }

     function isFloat($n){
          return ( $n == strval(floatval($n)) )? true : false;
     }


     function ValidatePassword($Pwd){
          if (strlen($Pwd)<5) {
               return false;
          }
          return true;
	}

     function IsValidDate($str) {
          $stamp = strtotime($str);

          if (!is_numeric($stamp)) {
               return FALSE;
          }
          $month = date('m', $stamp);
          $day = date('d', $stamp);
          $year = date('Y', $stamp);

          if (checkdate($month, $day, $year)) {
               return TRUE;
          }

          return FALSE;
     }

     function IsEmpty($value='') {
          if (trim($value) == '') {
               return true;
          }
          return false;
     }

     function clearSessionErrorMessages() {
          if ($_SESSION) {
               if (isset($_SESSION['error'])) {
                    unset($_SESSION['error']);
               }
          }
     }

}

function pr($array, $exit = false) {
     echo "<pre>";
     print_R($array);
     echo "</pre>";
     if ($exit) {
          exit;
     }
}

function mysqlRealEscape($str = '') {
     if ($str == '') {
          return;
     }
     return mysql_real_escape_string($str);
}

function htmlEntityDecode($str = '') {
     if ($str == '') {
          return;
     }
     return html_entity_decode($str, ENT_QUOTES);
}

function htmlEntity($str = '') {
     if ($str == '') {
          return;
     }
     return htmlentities($str, ENT_QUOTES);
}

function TrimElement($str = '') {
     if ($str == '') {
          return;
     }
     return trim($str);
}

function StripSlashElement($str = '') {
     if ($str == '') {
          return;
     }
     return stripslashes($str);
}

?>
